package com.puppet.controller;

import com.puppet.config.MinioUtils;
import com.puppet.constant.Result;
import com.puppet.constant.UploadImgConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("api")
public class MinioUploadController {

    @Autowired
    MinioUtils minioUtils;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostMapping("/upload")
    public Result uploadFile(@RequestParam MultipartFile file) {
        // 生成随机文件名
        String fileName = UUID.randomUUID().toString().substring(0, 5);

        // 上传文件到 Minio，保持原有逻辑
        fileName = fileName.substring(0,5);
        minioUtils.upload(file, fileName);

        // 获取文件URL
        String fileUrl = minioUtils.getFileUrl(fileName);

        //存储到redis
        stringRedisTemplate.opsForSet().add(UploadImgConstant.allUploadImgs,fileName);

        // 返回上传结果
        return new Result(200, "上传成功", fileUrl);
    }
}
